package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.User2Content;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMySaleProductController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMySaleProductController.class.getName());

	private IUserDao userDao;

	private IUserBusiness userBusiness;

	private SkinUtils skinUtils;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		User2Content u2c = userDao.findUser2ContentBy(u2csid);
		fb.setUser2Content(u2c);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		User2Content s2c = form.getUser2Content();
		long[] delgatedStoreSysIds = ServletRequestUtils.getLongParameters(request, "siteSysId");
		userBusiness.updateUserSaleContents(s2c, delgatedStoreSysIds);
		return mv;
	}

	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		// --------------------
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		// List<Site2User_Content> user2Content2SiteList =
		// userBusiness.getListUser2ContentDelegatedSite(u2csid, pgno,
		// pgsize);
		// //---------------
		// List<User2Site> user2StoreList =
		// userBusiness.getListUserFavoriteStore(auth.getUserSysId(), -1, 0,
		// 20);
		// HashMap<Long,String> delegatedFlags = new HashMap();
		// if (user2StoreList != null &&user2Content2SiteList!=null) {
		// for (int i = 0; i < user2StoreList.size(); i++) {
		// User2Site u2s = user2StoreList.get(i);
		// for(int j=0;j<user2Content2SiteList.size();j++){
		// Site2User_Content u2c2s = user2Content2SiteList.get(j);
		// if(u2c2s.getSite().getSysId().compareTo(u2s.getSite().getSysId())==0){
		// delegatedFlags.put(u2s.getSite().getSysId(), "Y");
		// }
		// }
		// }
		//
		// }
		// List user2StoreGroupList = UtilDataGroup.makeGroups(user2StoreList,
		// 0, 4);
		// model.put("user2StoreGroupList", user2StoreGroupList);
		// model.put("delegatedFlags", delegatedFlags);
		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMySaleProductController.logger = logger;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

}
